# -*- encoding: utf-8 -*-
# @File    :   article.py
# @Time    :   2021/07/27 12:21:03
# @Author  :   wudongmin
# @Contact :   wudongmin@xroom.net
# @version   :  1.0

from app.service import ServiceBase
from app.db.article import RepositoryArticle
import requests

class ServiceArticle(ServiceBase):
    # service article

    def __init__(self) -> None:
        self._db = RepositoryArticle()
        super().__init__()

    def get_article_type_list(self) -> dict:
        # 获取article类型列表
        res = self._db.get_type_list()
        return res

    def get_article_list(self, page_params=None, order_params=None) -> dict:
        # 获取article列表
        l, count = self._db.get_list(page_params=page_params, order_params=order_params)
        return dict(list=l, total=count)

    def get_article_detail(self, id: int) -> dict:
        # 获取article详情
        res = self._db.get_detail(id)
        return res

    def get_animation_list(self)->dict:
        # 获取我追的番剧列表
        url = "https://api.bilibili.com/x/space/bangumi/follow/list?type=1&follow_status=0&pn=1&ps=20&vmid=10836032&ts=1627627709859"
        data = requests.get(url).json()["data"]
        anlist = []
        for item in data["list"]:
            temp = dict(title=item["title"], cover=item["cover"], evaluate=item["evaluate"], url=item["url"])
            an = self._db.get_animation_detail(temp["title"])
            if an is not None:
                temp["cover"] = an.cover_url
            anlist.append(temp)
        return anlist